package servlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AdminDao;
import util.MD5;
import vo.Admin;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//1.收集信息
		String account=request.getParameter("account");
		String password=request.getParameter("password");
		//2.调用模型进行信息处理
		AdminDao dao=new AdminDao();
		try {
			Admin admin=dao.login(account,password);
			if (admin!=null){
				request.getSession().setAttribute("account", account);
				response.addCookie(new Cookie("account", URLEncoder.encode(account)));
				response.addCookie(new Cookie("password",URLEncoder.encode(password)));
				response.sendRedirect("loginSuccess.jsp");
			}else{
				response.sendRedirect("login.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
